﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NetMessage;

namespace CreateNetMessage
{
    public partial class UCMessage : UserControl
    {
        public UCMessage()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 控件初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UCMessage_Load(object sender, EventArgs e)
        {
            cPlusTypeBindTypeBindingSource.DataSource = BindComboxEnumType<CPlusType>.BindTyps;
            cbDirection.DataSource = BindComboxEnumType<MessageDirection>.BindTyps;
            cbDirection.DisplayMember = "Name";
            cbDirection.ValueMember = "Type";
        }

        public void BindObjcet(NetMessage.Message msg)
        {
            MessagebindingSource.DataSource = msg;
        }

        private void BtnAddParamClick(object sender, EventArgs e)
        {
            paramsBindingSource.Add(new Paramter());
        }

    }

    /// <summary>
    /// 用于Combox显示绑定的对象
    /// </summary>
    public class BindComboxEnumType<T>
    {
        /// <summary>
        /// 类型的名字
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 类型
        /// </summary>
        public T Type { get; set; }

        private static readonly List<BindComboxEnumType<T>> bindTyps;

        static BindComboxEnumType()
        {
            bindTyps = new List<BindComboxEnumType<T>>();

            foreach(var name in Enum.GetNames(typeof(T)))
            {
                bindTyps.Add(new BindComboxEnumType<T>()
                                 {
                                     Name = name,
                                     Type = (T)Enum.Parse(typeof(T), name)
                                 });
            }
        }

        /// <summary>
        /// 绑定的类型数据
        /// </summary>
        public static List<BindComboxEnumType<T>> BindTyps
        {
            get { return bindTyps; }
        }
    }
}
